package logic

import (
	"context"
	"tiktok/model/mysql"
	"tiktok/tiktokrpc/internal/svc"
	"tiktok/tiktokrpc/tiktokrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommentListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentListLogic {
	return &CommentListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CommentListLogic) CommentList(in *tiktokrpc.CommentListRequest) (*tiktokrpc.CommentListResponse, error) {
	// todo: add your logic here and delete this line
	var tikTokComments []mysql.TikTokComment

	mysql.DB.Debug().Preload("TikTokUser").Where("tik_tok_content_id = ?", in.ContentId).Find(&tikTokComments)

	var commentLists []*tiktokrpc.CommentList

	for _, comment := range tikTokComments {
		list := tiktokrpc.CommentList{
			Content: comment.Content,
			Name:    comment.TikTokUser.Name,
		}

		commentLists = append(commentLists, &list)
	}

	return &tiktokrpc.CommentListResponse{
		List: commentLists,
	}, nil
}
